import { DEFAULT_VALUE, localStorageFactory } from 'helpers/localStorage'
import { IMyMusic } from 'apis/types/business'
import Item from 'components/Layout/Header/Searcher/SearchResult/Item';

export const PLAY_LIST = '__playList';
export const PLAY_HISTORY = '__playHistory'
export const PLAY_MODE = '__playMode'
export enum KEY {
  PLAY_HISTORY,
  PLAY_LIST,
  PLAY_MODE,
}
export enum MODE {
  PLAY_IN_ORDER = 'PLAY_IN_ORDER',
  SINGLE_CYCLE = 'SINGLE_CYCLE',
  SHUFFLE_PLAYBACK = 'SHUFFLE_PLAYBACK',
}

export const playHistoryLocalStorage = localStorageFactory({
  key: KEY.PLAY_HISTORY,
  defaultValue: DEFAULT_VALUE.ARRAY,
})
export const setPlayHistory = (music: IMyMusic): IMyMusic[] => {
  const list = playHistoryLocalStorage.getItem().slice(0, 100);
  const index = list.findIndex(Item => Item.id === music.id);
  if (index > -1) {
    list.splice(index, 1);
  }
  list.unshift(music);
  playHistoryLocalStorage.setItem(list);
  return list
}
export const
  playListLocalStorage = localStorageFactory({
    key: KEY.PLAY_LIST,
    defaultValue: DEFAULT_VALUE.ARRAY
  })
export const playModeLocalStorage = localStorageFactory({
  key: KEY.PLAY_MODE,
  raw: true,
  defaultValue: MODE.PLAY_IN_ORDER
})